

#region copyright notice
/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2009 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

using UncleGED.Utils;
using UncleGED.Application;

namespace UGed
{
	public static class ConfigReader
	{

		#region Config Settings
		/// <summary>
		/// Summary of SaveXmlSettings.
		/// </summary>
		/// <param name=sXmlConfig></param>
		/// <param name=ConfigSettings></param>
		/// 		
		public static void SaveProject(string projectFile, UGedProject projectData)
		{
			XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
			FileStream fStream = new FileStream(projectFile, FileMode.Create);
			XmlS.Serialize(fStream, projectData);
			fStream.Close();
		}

		/// <summary>
		/// Summary of LoadXmlSettings.
		/// </summary>
		/// <param name=sXmlConfig></param>
		/// 		
		public static UGedProject LoadProject(string projectFile)
		{
			UGedProject configSettings;

			try
			{
				XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
				FileStream fStream = new FileStream(projectFile, FileMode.Open);
				configSettings = (UGedProject)XmlS.Deserialize(fStream);
				fStream.Close();
				fStream = null;
				XmlS = null;
			}
			catch
			{
				configSettings = new UGedProject();
				SaveProject(projectFile, configSettings);
			}

			return configSettings;

		}
		#endregion

		#region  Local application
		public static void SaveSettings(string SettingsFile, LocalApplication SettingsData)
		{
			XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
			FileStream fStream = new FileStream(SettingsFile, FileMode.Create);
			XmlS.Serialize(fStream, SettingsData);
			fStream.Close();
		}

		/// <summary>
		/// Summary of LoadXmlSettings.
		/// </summary>
		/// <param name=sXmlConfig></param>
		/// 		
		public static LocalApplication LoadSettings(string SettingsFile)
		{
			LocalApplication configSettings;

			try
			{
				XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
				FileStream fStream = new FileStream(SettingsFile, FileMode.Open);
				configSettings = (LocalApplication)XmlS.Deserialize(fStream);
				fStream.Close();
				fStream = null;
				XmlS = null;
			}
			catch
			{
				configSettings = new LocalApplication();
				SaveSettings(SettingsFile, configSettings);
			}

			return configSettings;

		}

		#endregion

		//this copies default files on initial start-up
		public static void CopyDefaultFiles()
		{
			string uPath = FolderMethods.GetUserPath();
			string aPath = FolderMethods.GetAppPath();
			string testFile = "";
			string from = "";
			string to = "";

			try
			{
				testFile = "tagtotext.txt";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_basic.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_ged55.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_generic.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_leg.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_paf.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "gedcom\\temp_paf4.ged";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "css\\uged.css";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "html\\uged.css";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "html\\images\\ug_banner.gif";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "html\\images\\ug_bkgrd.gif";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "sample css\\uged_1.css";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "sample css\\uged_2.css";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "sample css\\uged_3.css";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "images\\rss.jpg";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "images\\addtogoogle.png";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);

				testFile = "images\\rss_rect.png";
				from = aPath + "\\" + testFile;
				to = uPath + "\\" + testFile;
				CopyIfNotFound(from, to);
			}
			catch 
			{
				//empty catch
			}
		}

		private static void CopyIfNotFound(string from, string to)
		{
			try
			{
				if (File.Exists(to) == false)
				{
					File.Copy(from, to);
				}
			}
			catch 
			{
				//empty catch
			}
		}

	}
}
